/*
 * 文件名称: soft_gain.c
 * 功能描述: 本文件为软件增益处理模块
 ****************************************************************************************
 */
#include "include.h"
#include "soft_gain.h"

//static soft_gain_cfg_t soft_gain_cfg;
static u32 soft_gain = SOFT_GAIN_P0_0DB;        //默认数字增益

AT(.rodata.soft_gain_tbl)
static const u32 soft_gain_tbl_64[128] = {
    SOFT_GAIN_P0_0DB, SOFT_GAIN_P0_5DB, SOFT_GAIN_P1_0DB, SOFT_GAIN_P1_5DB, SOFT_GAIN_P2_0DB,
    SOFT_GAIN_P2_5DB, SOFT_GAIN_P3_0DB, SOFT_GAIN_P3_5DB, SOFT_GAIN_P4_0DB, SOFT_GAIN_P4_5DB,
    SOFT_GAIN_P5_0DB, SOFT_GAIN_P5_5DB, SOFT_GAIN_P6_0DB, SOFT_GAIN_P6_5DB, SOFT_GAIN_P7_0DB,
    SOFT_GAIN_P7_5DB, SOFT_GAIN_P8_0DB, SOFT_GAIN_P8_5DB, SOFT_GAIN_P9_0DB, SOFT_GAIN_P9_5DB,
    SOFT_GAIN_P10_0DB, SOFT_GAIN_P10_5DB, SOFT_GAIN_P11_0DB, SOFT_GAIN_P11_5DB, SOFT_GAIN_P12_0DB,
    SOFT_GAIN_P12_5DB, SOFT_GAIN_P13_0DB, SOFT_GAIN_P13_5DB, SOFT_GAIN_P14_0DB, SOFT_GAIN_P14_5DB,
    SOFT_GAIN_P15_0DB, SOFT_GAIN_P15_5DB, SOFT_GAIN_P16_0DB, SOFT_GAIN_P16_5DB, SOFT_GAIN_P17_0DB,
    SOFT_GAIN_P17_5DB, SOFT_GAIN_P18_0DB, SOFT_GAIN_P18_5DB, SOFT_GAIN_P19_0DB, SOFT_GAIN_P19_5DB,
    SOFT_GAIN_P20_0DB, SOFT_GAIN_P20_5DB, SOFT_GAIN_P21_0DB, SOFT_GAIN_P21_5DB, SOFT_GAIN_P22_0DB,
    SOFT_GAIN_P22_5DB, SOFT_GAIN_P23_0DB, SOFT_GAIN_P23_5DB, SOFT_GAIN_P24_0DB, SOFT_GAIN_P24_5DB,
    SOFT_GAIN_P25_0DB, SOFT_GAIN_P25_5DB, SOFT_GAIN_P26_0DB, SOFT_GAIN_P26_5DB, SOFT_GAIN_P27_0DB,
    SOFT_GAIN_P27_5DB, SOFT_GAIN_P28_0DB, SOFT_GAIN_P28_5DB, SOFT_GAIN_P29_0DB, SOFT_GAIN_P29_5DB,
    SOFT_GAIN_P30_0DB, SOFT_GAIN_P30_5DB, SOFT_GAIN_P31_0DB, SOFT_GAIN_P31_5DB,
    //负增益
    SOFT_GAIN_N0DB,SOFT_GAIN_N1DB,SOFT_GAIN_N2DB,SOFT_GAIN_N3DB,SOFT_GAIN_N4DB,
    SOFT_GAIN_N5DB,SOFT_GAIN_N6DB,SOFT_GAIN_N7DB,SOFT_GAIN_N8DB,SOFT_GAIN_N9DB,
    SOFT_GAIN_N10DB,SOFT_GAIN_N11DB,SOFT_GAIN_N12DB,SOFT_GAIN_N13DB,SOFT_GAIN_N14DB,
    SOFT_GAIN_N15DB,SOFT_GAIN_N16DB,SOFT_GAIN_N17DB,SOFT_GAIN_N18DB,SOFT_GAIN_N19DB,
    SOFT_GAIN_N20DB,SOFT_GAIN_N21DB,SOFT_GAIN_N22DB,SOFT_GAIN_N23DB,SOFT_GAIN_N24DB,
    SOFT_GAIN_N25DB,SOFT_GAIN_N26DB,SOFT_GAIN_N27DB,SOFT_GAIN_N28DB,SOFT_GAIN_N29DB,
    SOFT_GAIN_N30DB,SOFT_GAIN_N31DB,SOFT_GAIN_N32DB,SOFT_GAIN_N33DB,SOFT_GAIN_N34DB,
    SOFT_GAIN_N35DB,SOFT_GAIN_N36DB,SOFT_GAIN_N37DB,SOFT_GAIN_N38DB,SOFT_GAIN_N39DB,
    SOFT_GAIN_N40DB,SOFT_GAIN_N41DB,SOFT_GAIN_N42DB,SOFT_GAIN_N43DB,SOFT_GAIN_N44DB,
    SOFT_GAIN_N45DB,SOFT_GAIN_N46DB,SOFT_GAIN_N47DB,SOFT_GAIN_N48DB,SOFT_GAIN_N49DB,
    SOFT_GAIN_N50DB,SOFT_GAIN_N51DB,SOFT_GAIN_N52DB,SOFT_GAIN_N53DB,SOFT_GAIN_N54DB,
    SOFT_GAIN_N55DB,SOFT_GAIN_N56DB,SOFT_GAIN_N57DB,SOFT_GAIN_N58DB,SOFT_GAIN_N59DB,
    SOFT_GAIN_N60DB,SOFT_GAIN_N65DB,SOFT_GAIN_N110DB,SOFT_GAIN_N110DB,
};

///软件增益放到DRC里面去处理,可以有效地对增益范围进行保护
AT(.text.soft_gain_get)
u32 soft_gain_param_get(void)
{
    return soft_gain;
}

AT(.text.soft_gain_set)
void soft_gain_set(u8 gain)
{
    if(gain >= 127){
        gain = 127;
    }
    soft_gain = soft_gain_tbl_64[gain];
}


///软件增益处理(主频160M，120个点，算法时间在21us左右)
//AT(.text.soft_gain)
//void soft_gain_audio_input(u8 *ptr, u32 samples, int ch_mode, void *params)
//{
//    if(soft_gain_cfg.enable) {
//        s16 *rptr = (s16 *)ptr;
//        s32 temp  = 0;
//
//        for(int i = 0; i < samples; i++) {
//            temp = rptr[i];
//            temp = __builtin_muls_shift15(temp, soft_gain_cfg.gain);
//            if(temp > 32767) {
//                rptr[i] = 32767;
//            } else if(temp < -32767) {
//                rptr[i] = -32767;
//            } else {
//                rptr[i] = temp;
//            }
//        }
//    }
//
//    if (soft_gain_cfg.callback) {
//        soft_gain_cfg.callback(ptr, samples, ch_mode, params);
//    }
//}

//AT(.text.soft_gain_set)
//void soft_gain_audio_output_callback_set(audio_callback_t callback)
//{
//    soft_gain_cfg.callback = callback;
//}
//
//AT(.text.soft_gain_init)
//void soft_gain_init(u8 sample_rate, u16 samples)
//{
//    memset(&soft_gain_cfg, 0, sizeof(soft_gain_cfg));
//    soft_gain_set(xcfg_cb.mic_soft_gain);
//    soft_gain_cfg.samples = samples;
//    soft_gain_cfg.enable  = 1;
//}
//
//AT(.text.soft_gain_exit)
//void soft_gain_exit(void)
//{
//    soft_gain_cfg.enable = 0;
//}
